package com.health.mapp.utils;
 


/*
 *  Copyright  sunflower
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * 日期时间工具类
 * 
 * @author sunflower
 * 
 */
public class DateUtils {
	private static final SimpleDateFormat datetimeFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd");
	
	private static final SimpleDateFormat dateFormat2 = new SimpleDateFormat(
			"yyyyMMdd");
	
	private static final SimpleDateFormat timeFormat = new SimpleDateFormat(
			"HH:mm:ss");
	private static final SimpleDateFormat datetimeFormaHHmm = new SimpleDateFormat(
	"yyyy-MM-dd HH:mm");

 

	
	
	/**
	 * 获得当前日期时间
	 * <p>
	 * 日期时间格式yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 */
	public static String currentDatetime() {
		return datetimeFormat.format(now());
	}
	
	/**
	 * 获得当前日期时间
	 * <p>
	 * 日期时间格式yyyyMMddHHmmss
	 * 
	 * @return
	 */
	public static String currentDatetimeNoSeparator() {
		
		 SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
	     return format.format(now());
	}
	
	  /**
     * 两个时间段的间隔秒数
     * @return
     */
    public static int currentTimeSolt()  
    { 
    	Long longTime = millis();
    	String date = currentDate()+" 00:00:00";
    	Long zeroDate = parseDatetime(date).getTime();
    	int  solt = (int) ((longTime - zeroDate)/(1000*60*30))+1;
    	return  solt;
    }
	

	/**
	 * 格式化日期时间
	 * <p>
	 * 日期时间格式yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 */
	public static String formatDatetime(Date date) {
		
		//已用到
		return datetimeFormat.format(date);
	}

	/**
	 * 格式化日期时间
	 * 
	 * @param date
	 * @param pattern
	 *            格式化模式，详见{@link SimpleDateFormat}构造器
	 *            <code>SimpleDateFormat(String pattern)</code>
	 * @return
	 */
	public static String formatDatetime(Date date, String pattern) {
		SimpleDateFormat customFormat = (SimpleDateFormat) datetimeFormat
				.clone();
		customFormat.applyPattern(pattern);
		return customFormat.format(date);
	}

	/**
	 * 获得当前日期
	 * <p>
	 * 日期格式yyyy-MM-dd
	 * 
	 * @return
	 */
	public static String currentDate() {
		return dateFormat.format(now());
	}

	/**
	 * 获得当前日期
	 * <p>
	 * 日期格式yyyyMMdd
	 * 
	 * @return
	 */
	public static String todayDate() {
		return dateFormat2.format(now());
	}
	
	/**
	 * 格式化日期
	 * <p>
	 * 日期格式yyyy-MM-dd
	 * 
	 * @return
	 */
	public static String formatDate(Date date) {
		return dateFormat.format(date);
	}

	/**
	 * 获得当前时间
	 * <p>
	 * 时间格式HH:mm:ss
	 * 
	 * @return
	 */
	public static String currentTime() {
		return timeFormat.format(now());
	}

	/**
	 * 格式化时间
	 * <p>
	 * 时间格式HH:mm:ss
	 * 
	 * @return
	 */
	public static String formatTime(Date date) {
		return timeFormat.format(date);
	}
	/**
	 * 获取当前时间
	 * @return	当前的时间
	 */
	public static String getNow(){
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
	}
 
	/**
	 * 获得当前时间的<code>java.util.Date</code>对象
	 * 
	 * @return
	 */
	public static Date now() {
		return new Date();
	}
	/**
	 * 1970年以来的毫米数转为Date
	 * @param currentTimeMillis
	 * @return
	 */
	public static Date now(long currentTimeMillis) {
		
		return new  Date(currentTimeMillis);
 
	}
	public static Calendar calendar() {
		Calendar cal = GregorianCalendar.getInstance(Locale.CHINESE);
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		return cal;
	}

	/**
	 * 获得当前时间的毫秒数
	 * <p>
	 * 详见{@link System#currentTimeMillis()}
	 * 
	 * @return
	 */
	public static long millis() {
		return System.currentTimeMillis();
	}

	
	/**
	 * 获得当前时间的毫秒数
	 * <p>
	 * 详见{@link System#currentTimeMillis()}
	 * 
	 * @return
	 */
	public static long millisSecond() {
		return System.currentTimeMillis()/1000;
	}
	/**
	 * 
	 * 获得当前Chinese月份
	 * 
	 * @return
	 */
	public static int month() {
		return calendar().get(Calendar.MONTH) + 1;
	}

	/**
	 * 获得月份中的第几天
	 * 
	 * @return
	 */
	public static int dayOfMonth() {
		return calendar().get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 今天是星期的第几天
	 * 
	 * @return
	 */
	public static int dayOfWeek() {
		return calendar().get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 今天是年中的第几天
	 * 
	 * @return
	 */
	public static int dayOfYear() {
		return calendar().get(Calendar.DAY_OF_YEAR);
	}

	/**
	 *判断原日期是否在目标日期之前
	 * 
	 * @param src
	 * @param dst
	 * @return
	 */
	public static boolean isBefore(Date src, Date dst) {
		return src.before(dst);
	}

	/**
	 *判断原日期是否在目标日期之后
	 * 
	 * @param src
	 * @param dst
	 * @return
	 */
	public static boolean isAfter(Date src, Date dst) {
		return src.after(dst);
	}

	/**
	 *判断两日期是否相同
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isEqual(Date date1, Date date2) {
		return date1.compareTo(date2) == 0;
	}

	/**
	 * 判断某个日期是否在某个日期范围
	 * 
	 * @param beginDate
	 *            日期范围开始
	 * @param endDate
	 *            日期范围结束
	 * @param src
	 *            需要判断的日期
	 * @return
	 */
	public static boolean between(Date beginDate, Date endDate, Date src) {
		return beginDate.before(src) && endDate.after(src);
	}

	/**
	 * 获得当前月的最后一天
	 * <p>
	 * HH:mm:ss为0，毫秒为999
	 * 
	 * @return
	 */
	public static Date lastDayOfMonth() {
		//已用到
		Calendar cal = calendar();
		//月份的起始值为0而不是１，所以要设置八月时，我们用7而不是8
		cal.set(Calendar.DAY_OF_MONTH, 1); // MM置零
		cal.set(Calendar.HOUR_OF_DAY, 0);// HH置零
		cal.set(Calendar.MINUTE, 0);// mi置零
		cal.set(Calendar.SECOND, 0);// ss置零
		cal.set(Calendar.MILLISECOND, 0);// S置零
		//月份的起始值为0而不是１，所以要设置八月时，我们用7而不是8
		cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);// 月份+1
		cal.set(Calendar.MILLISECOND, -1);// 毫秒-1
		return cal.getTime();
	}

	/**
	 * 获得当前月的第一天
	 * <p>
	 * HH:mm:ss SS为零
	 * 
	 * @return
	 */
	public static Date firstDayOfMonth() {
		//已用到
		Calendar cal = calendar();
		cal.set(Calendar.DAY_OF_MONTH, 1); // MM置1
		cal.set(Calendar.HOUR_OF_DAY, 0);// HH置零
		cal.set(Calendar.MINUTE, 0);// mi置零
		cal.set(Calendar.SECOND, 0);// ss置零
		cal.set(Calendar.MILLISECOND, 0);// S置零
		return cal.getTime();
	}

	
	

	
	public static Date weekDay(int week) {
		Calendar cal = calendar();
		cal.set(Calendar.DAY_OF_WEEK, week);
		return cal.getTime();
	}

	/**
	 * 获得周五日期
	 * <p>
	 * 注：日历工厂方法{@link #calendar()}设置类每个星期的第一天为Monday，US等每星期第一天为sunday
	 * 
	 * @return
	 */
	public static Date friday() {
		return weekDay(Calendar.FRIDAY);
	}

	/**
	 * 获得周六日期
	 * <p>
	 * 注：日历工厂方法{@link #calendar()}设置类每个星期的第一天为Monday，US等每星期第一天为sunday
	 * 
	 * @return
	 */
	public static Date saturday() {
		return weekDay(Calendar.SATURDAY);
	}

	/**
	 * 获得周日日期
	 * <p>
	 * 注：日历工厂方法{@link #calendar()}设置类每个星期的第一天为Monday，US等每星期第一天为sunday
	 * 
	 * @return
	 */
	public static Date sunday() {
		return weekDay(Calendar.SUNDAY);
	}

	/**
	 * 将字符串日期时间转换成java.util.Date类型
	 * <p>
	 * 日期时间格式yyyy-MM-dd HH:mm:ss
	 * 
	 * @param datetime
	 * @return
	 */
	public static Date parseDatetime(String datetime)   {
		Date dt=null;
		try {
			dt= datetimeFormat.parse(datetime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dt;
	}

	/**
	 * 返回年月日时分秒的Date类型
	 * @return
	 */
	public static Date getDatetime()   {
		Date dt=null;
		try {
			dt= datetimeFormat.parse(currentDatetime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dt;
	}
	
	/**
	 * 将字符串日期转换成java.util.Date类型
	 *<p>
	 * 日期时间格式yyyy-MM-dd
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date parseDate(String date) throws ParseException {
		return dateFormat.parse(date);
	}

	/**
	 * 将字符串日期转换成java.util.Date类型
	 *<p>
	 * 时间格式 HH:mm:ss
	 * 
	 * @param time
	 * @return
	 * @throws ParseException
	 */
	public static Date parseTime(String time) throws ParseException {
		return timeFormat.parse(time);
	}

	/**
	 * 根据自定义pattern将字符串日期转换成java.util.Date类型
	 * 
	 * @param datetime
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static Date parseDatetime(String datetime, String pattern)
			throws ParseException {
		SimpleDateFormat format = (SimpleDateFormat) datetimeFormat.clone();
		format.applyPattern(pattern);
		return format.parse(datetime);
	}

	/**
	 * 获得昨天的日期
	 * <p>
	 * 日期时间格式yyyy-MM-dd
	 * 
	 * @return
	 */
	public static String yesterdayDate() {
		Calendar   cal   =   Calendar.getInstance();
		  cal.add(Calendar.DATE,   -1);

		return dateFormat.format(cal.getTime());
	}

	/**判断是否超过24小时

	   *   

	   * @param date1

	   * @param date2

	   * @return boolean

	   * @throws Exception

	   */

	    public static boolean judgmentDate(String date1, String date2,int gapHour) throws Exception { 
	    	if(null==date1 || null==date2 )
	    	{
	    		return false;
	    	}
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d HH:mm:ss"); 

	        Date start = sdf.parse(date1); 

	        Date end = sdf.parse(date2); 

	        long cha = end.getTime() - start.getTime(); 

	        if(cha<0){

	          return false; 

	        }

	        double result = cha * 1.0 / (1000 * 60 * 60);

	        if(result<=gapHour){ 

	             return true; 

	        }else{ 

	             return false; 

	        } 

	    }
	    /**判断是否超过24小时

		   *   

		   * @param date1

		   * @param date2

		   * @return boolean

		   * @throws Exception

		   */

		    public static boolean judgmentDate(Date date1, Date date2,double gapHour)  
		    { 

		      
		    	if(null==date1 || null==date2 )
		    	{
		    		return false;
		    	}
		        Date start = date1; 

		        Date end = date2; 

		        long cha = end.getTime() - start.getTime(); 

		        if(cha<0){

		          return false; 

		        }
 
		        double result = cha * 1.0 / (1000 * 60 * 60);

		        if(result<=gapHour){ 

		             return true; 

		        }else{ 

		             return false; 

		        } 

		    }
		    
		    public static boolean judgmentDateAbs(String date1, String date2,int gapHour) throws Exception  
		    { 
		    	if(null==date1 || null==date2 )
		    	{
		    		return false;
		    	}
		    	  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d HH:mm:ss"); 

			        Date start = sdf.parse(date1); 

			        Date end = sdf.parse(date2); 

			        long cha =  Math.abs(end.getTime() - start.getTime()); 

			        double result = cha * 1.0 / (1000 * 60 * 60);

			        if(result<=gapHour){ 

			             return true; 

			        }else{ 

			             return false; 

			        } 


		    }
		    
		    /**
		     * 判断日期间隔大小
		     * @param date1
		     * @param date2
		     * @param gapHour 间隔 
		     * @return
		     */
		    public static boolean judgmentDateAbs(Date date1, Date date2,int gapHour)  
		    { 

		    	if (null ==date1||null==date2) {
		    		return false; 
				}
		    	
		        Date start = date1; 

		        Date end = date2; 

		        long cha = Math.abs( end.getTime() - start.getTime()); 

		        double result = cha * 1.0 / (1000 * 60 * 60);

		        if(result<=gapHour){ 

		             return true; 

		        }else{ 

		             return false; 

		        } 

		    }
		    
		    /**
		     * 两个时间段的间隔秒数
		     * @param date1
		     * @param date2
		     * @return
		     */
		    public static int judgmentDateBetween(Date date1, Date date2)  
		    { 
		    	if(null==date1 || null==date2 )
		    	{
		    		return -1;
		    	}
		        Date start = date1; 
		        
		        Date end = date2; 

		        long cha = Math.abs( end.getTime() - start.getTime()); 
		        
		        return (int) (cha/1000);
		    }
		    
		  
		    public static Date getDateAfter(Date d, int day) {  
		        Calendar now = Calendar.getInstance();  
		        now.setTime(d);  
		        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);  
		        return now.getTime();  
		    }
		    
		    
		    public static int dayOfWeek(String pTime) throws Exception {
		    	  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		    	  Calendar c = Calendar.getInstance();
		    	  c.setTime(format.parse(pTime));
		    	  int dayForWeek = 0;
		    	  if(c.get(Calendar.DAY_OF_WEEK) == 1){
		    	   dayForWeek = 7;
		    	  }else{
		    	   dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
		    	  }
		    	  return dayForWeek;
		    }
		    
		    
		    
		    public static Date getDateAfterSecond(Date d, int second) {  
		        Calendar now = Calendar.getInstance();  
		        now.setTime(d);  
		        now.add(Calendar.MINUTE, second);
		        return now.getTime();  
		    }
		    
		    
		    /**
		     * 返回month个月后的时间
		     * @param d 时间
		     * @param month 
		     * @return
		     */
		    public static Date getMonthAfter(Date d, int month) {  
		        Calendar now = Calendar.getInstance();  
		        now.setTime(d);  
		        now.set(Calendar.MONTH, now.get(Calendar.MONTH) + month);  
		        return now.getTime();  
		    }
		    
		    //date.setTime(date.getTime() - 4 * 60 * 1000);
		    
		    
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
	
	System.out.println(DateUtils.getMonthAfter(DateUtils.now(), 15));
		
		/*int today = DateUtils.dayOfWeek();
		for (int i = 0; i < 7; i++) {
			System.out.println((today+i-1)%7);
		}
		
	for (int i = 0; i < 7; i++) {
			System.out.println(today+i-1);
			System.out.println(DateUtils.formatDatetime(DateUtils.weekDay(today+i)));
		}*/
		
		
	/*	System.out.println(DateUtils.formatDatetime(DateUtils.weekDay(2)));
		System.out.println(DateUtils.formatDatetime(DateUtils.weekDay(3)));
		System.out.println(DateUtils.formatDatetime(DateUtils.weekDay(4)));
		System.out.println(DateUtils.formatDatetime(DateUtils.weekDay(5)));
		System.out.println(DateUtils.formatDatetime(DateUtils.weekDay(6)));
		System.out.println(DateUtils.formatDatetime(DateUtils.weekDay(7)));
		System.out.println(DateUtils.formatDatetime(DateUtils.weekDay(8)));
		System.out.println(DateUtils.formatDatetime(DateUtils.weekDay(0)));*/
		
		//System.out.println(judgmentDateAbs("2017-03-23 11:60:00", "2017-03-23 10:00:00", 1));
		
	}

}